import { useReducer } from "react"
import { createContext } from "react"
import RouterConfig from "./router/RouterConfig"

// // 使用全局包裹组件,实现消息提示的全局显示
// import { App as AntdApp } from 'antd'

// 1.创建context
export const Context = createContext()

function App() {

  // 2.创建reducer
  let [state, dispatch] = useReducer((state, { type, payload }) => {
    switch (type) {
      case "setToken":
        return {
          ...state,
          token: payload
        }
        break;
      case "setUserInfo":
        return {
          ...state,
          userInfo: {
            ...payload
          }
        }
        break;
      case "setNavPath":
        return {
          ...state,
          navPath: payload
        }
      default:
        break;
    }
  }, {
    // 初始化全局数据
    token: JSON.parse(localStorage.getItem("token")) || null,  // 将JSON字符串转换为对象
    userInfo: JSON.parse(localStorage.getItem("userInfo")) || null,
    navPath: null,
  })




  return (
    // 3.包裹全局
    <Context.Provider value={{ state, dispatch }}>
      {/* <AntdApp> */}
      <RouterConfig></RouterConfig>
      {/* </AntdApp> */}
    </Context.Provider>
  )
}

export default App
